package com.jxnu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*System.out.println("前置通知");
        return true; //false  拦截  true  放行*/
        System.out.println(111);
        Boolean flag = (Boolean) request.getSession().getAttribute("user");
        if(flag==null){
            flag=false;
        }
        if(flag){
            return true;
        }else{
        //  未登录  就拦截
            String contextPath = request.getServletContext().getContextPath();
            response.sendRedirect(contextPath+"/form.jsp");
            return false;
        }

    }

    /*@Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("后置通知");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("最终通知");
    }*/
}
